package com.leigq.basaltools.util;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import java.util.Objects;

/**
 * CookieUtils
 *
 * @author leigq
 * @date 2021-05-13 11:51:04
 */
public abstract class CookieUtils {

    public static final int COOKIE_MAX_AGE = 7 * 24 * 3600;
    public static final int COOKIE_HALF_HOUR = 30 * 60;

    /**
     * 根据Cookie名称得到Cookie对象，不存在该对象则返回Null
     *
     * @param name the name
     * @return cookie
     */
    public static Cookie getCookie(String name) {
        final Cookie[] cookies = WebUtils.getCookies();
        if (Objects.isNull(cookies) || cookies.length == 0) {
            return null;
        }
        Cookie cookie = null;
        for (Cookie c : cookies) {
            if (name.equals(c.getName())) {
                cookie = c;
                break;
            }
        }
        return cookie;
    }


    /**
     * 根据Cookie名称直接得到Cookie值
     *
     * @param name the name
     * @return cookie value
     */
    public static String getCookieValue(String name) {
        Cookie cookie = getCookie(name);
        if (cookie != null) {
            return cookie.getValue();
        }
        return null;
    }

    /**
     * 移除cookie
     *
     * @param name 这个是名称，不是值
     */
    public static void removeCookie(String name) {
        if (null == name) {
            return;
        }
        Cookie cookie = getCookie(name);
        if (null != cookie) {
            cookie.setPath("/");
            cookie.setValue("");
            cookie.setMaxAge(0);
            final HttpServletResponse response = WebUtils.getResponse();
            response.addCookie(cookie);
        }
    }

    /**
     * 添加一条新的Cookie，可以指定过期时间(单位：秒)
     *
     * @param name     the name
     * @param value    the value
     * @param maxValue the max value
     */
    public static void setCookie(String name, String value, int maxValue) {
        final Cookie cookie = generate(name, value, maxValue);
        if (cookie == null) {
            return;
        }
        final HttpServletResponse response = WebUtils.getResponse();
        response.addCookie(cookie);
    }


    /**
     * 添加一条新的Cookie，默认30分钟过期时间
     *
     * @param name  the name
     * @param value the value
     */
    public static void setCookie(String name, String value) {
        setCookie(name, value, COOKIE_HALF_HOUR);
    }


    /**
     * 添加一条新的Cookie，安全
     * <br/>
     * HttpOnly = true <br/>
     * Secure = true
     *
     * @param name  the name
     * @param value the value
     */
    public static void setSecurityCookie(String name, String value, int maxValue) {
        final Cookie cookie = generate(name, value, maxValue);
        if (cookie == null) {
            return;
        }
        cookie.setHttpOnly(true);
        cookie.setSecure(true);
        WebUtils.getResponse().addCookie(cookie);
    }

    /**
     * Generate cookie.
     *
     * @param name     the name
     * @param value    the value
     * @param maxValue the max value
     * @return the cookie
     */
    public static Cookie generate(String name, String value, int maxValue) {
        if (Objects.isNull(name) || name.trim().length() == 0) {
            return null;
        }
        Cookie cookie = new Cookie(name, value == null ? "" : value);
        cookie.setPath("/");
        cookie.setMaxAge(maxValue != 0 ? maxValue : COOKIE_HALF_HOUR);
        return cookie;
    }
}
